package com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation

import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkDialog.DialogEvaluationExport
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.JumpActivityHelp
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.LancooMethod
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.bindLifeCycle
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.ErrorRecovery
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.PaperEvaluationInfor
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.viewModel.MarkViewModel
import android.app.Activity
import android.content.Intent
import android.os.Environment
import android.os.Handler
import android.text.Html
import android.text.TextPaint
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseActivity
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseFragmentPagerAdapter
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkDialog.ShowDialogTools
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkPublicActivity.ActivityErrorRecovery
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionDXT.EvaluationFragmentDxt
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionFHXZ.EvaluationFragmentFhxz
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionGCT.EvaluationFragmentGct
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionJDT.EvaluationFragmentJdt
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionPPT.EvaluationFragmentPpt
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionTKT.EvaluationFragmentTkt
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionWXXZ.EvaluationFragmentWxxz
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionXZT.EvaluationFragmentXzt
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionYDPP.EvaluationFragmentYdpp
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionZHT.EvaluationFragmentZht
import com.lancoo.znbkxx.teacher.aibkTestCoach.paperToEvaluation.aibkEvalutionZW.EvaluationFragmentZw
import com.lancoo.znbkxx.teacher.model.OperationHelp
import com.lancoo.znbkxx.teacher.ui.activity.CommentDetailActivity
import com.lancoo.znbkxx.teacher.utils.DensityUtils
import com.lancoo.znbkxx.teacher.utils.ExportUtil
import com.lancoo.znbkxx.teacher.utils.LancooUtils
import kotlinx.android.synthetic.main.aibk_activity_paper_evaluation.*
import kotlinx.android.synthetic.main.aibk_activity_paper_evaluation.LlToolsView
import kotlinx.android.synthetic.main.aibk_activity_paper_evaluation.TvPaperName
import kotlinx.android.synthetic.main.aibk_activity_paper_evaluation.TvQuesInfor
import kotlinx.android.synthetic.main.aibk_dialog_evaluation_export.*
import kotlinx.android.synthetic.main.aibk_include_loading_refresh.*
import org.koin.androidx.viewmodel.ext.android.viewModel
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL

class AibkPaperEvaluationActivity : KBaseActivity(), View.OnClickListener {

    companion object {
        fun launch(activity: Activity, paperId: String,setId:String) {
            val intent = Intent(activity, AibkPaperEvaluationActivity::class.java)
            intent.putExtra("PaperID", paperId)
            intent.putExtra("SetID", setId)
            activity.startActivity(intent)
        }
    }

    private var quesScoreInfor = ""
    private var typeListPosition = 0
    private var quesListPosition = 0
    private var vpQuesPosition = 0
    private val mViewModel: MarkViewModel by viewModel()
    private var paperEvaluationInfor: PaperEvaluationInfor? = null
    private var fragmentList: MutableList<Fragment>? = ArrayList()

    override fun setLayout(): Int {
        return R.layout.aibk_activity_paper_evaluation
    }

    override fun initView() {
        BtnBack.setOnClickListener(this)
        BtnErrorRecovery.setOnClickListener(this)
        BtnErrorRecovery.visibility = View.GONE//设计人员更改设计建议屏蔽该功能
        BtnExport.setOnClickListener(this)
        BtnKnowledge.setOnClickListener(this)
        BtnAnswerSheet.setOnClickListener(this)
        TvPaperName.visibility = View.GONE
        LlToolsView.visibility = View.GONE
        TvQuesInfor.visibility = View.GONE
        TvQuesInfor.setOnClickListener(this)
    }

    override fun initData() {
        val paperId = intent.getStringExtra("PaperID")
        val setId = intent.getStringExtra("SetID")
        mViewModel.getPaperEvaluationInfor(paperId,setId)
                .doOnSubscribe {
                    RlGifLoading.visibility = View.VISIBLE
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        paperEvaluationInfor = it.data
                        showPaperEvaluationView(paperEvaluationInfor!!)
                        RlGifLoading.visibility = View.GONE
                    } else {
                        MyToast.showToast(it.message)
                        Handler().postDelayed({
                            finish()
                        }, 1000)
                    }
                }, {
                    MyToast.showToast(it.message)
                    Handler().postDelayed({
                        finish()
                    }, 1000)
                })
    }

    private fun showPaperEvaluationView(paperEvaluationInfor: PaperEvaluationInfor) {
        //显示试卷名称以及答题卡按钮
        val paperName = paperEvaluationInfor.PaperName
        val setName = paperEvaluationInfor.SetName
        TvPaperName.text = "$paperName($setName)的讲评"
        TvPaperName.visibility = View.VISIBLE
        LlToolsView.visibility = View.VISIBLE
        TvQuesInfor.visibility = View.VISIBLE


        //加载试题,显示试卷作答情况
        fragmentList?.clear()
        var typeListSize = paperEvaluationInfor.TypeList.size
        for (i in 0 until typeListSize) {
            var typeNo = paperEvaluationInfor.TypeList[i].TypeNo
            var quesListSize = paperEvaluationInfor.TypeList[i].QuesList.size
            for (j in 0 until quesListSize) {
                if (typeNo == "A") {
                    val mFragment = EvaluationFragmentDxt(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "C") {
                    val mFragment = EvaluationFragmentWxxz(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "L" ||typeNo == "E" || typeNo == "F" || typeNo == "D") {//听力选择，听力判断，阅读选择
                    val mFragment = EvaluationFragmentXzt(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "k") {
                    val mFragment = EvaluationFragmentZht(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                }else if (typeNo == "H") {
                    val mFragment = EvaluationFragmentFhxz(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "f") {
                    val mFragment = EvaluationFragmentZw(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "N" || typeNo == "S") { //N:普通填空题  S：听力填空题
                    val mFragment = EvaluationFragmentTkt(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "Q" || typeNo == "b" || typeNo == "d"
                        || typeNo == "X" || typeNo == "g" || typeNo == "V"
                        || typeNo == "G") {//Q:阅读填空,X:阅读简答,G:听力简答,
                    val mFragment = EvaluationFragmentJdt(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "x" || typeNo == "m") {//x:听力匹配,m:普通匹配题
                    val mFragment = EvaluationFragmentPpt(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "U") {//U:改错题
                    val mFragment = EvaluationFragmentGct(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                } else if (typeNo == "P") {//P:阅读匹配
                    val mFragment = EvaluationFragmentYdpp(this, paperEvaluationInfor, i, j)
                    (fragmentList as ArrayList<Fragment>).add(mFragment)
                }
            }
        }
        val mAdapter = KBaseFragmentPagerAdapter(supportFragmentManager, fragmentList as ArrayList<Fragment>)
        VpEvaluation.adapter = mAdapter

        //处理viewpager滑动事件
        VpEvaluation.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                    position: Int,
                    positionOffset: Float,
                    positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                vpQuesPosition = position
                showQuesTitleInfor(paperEvaluationInfor)
            }

            override fun onPageScrollStateChanged(state: Int) {}
        })

        showQuesTitleInfor(paperEvaluationInfor)
    }

    private fun showQuesTitleInfor(paperEvaluationInfor: PaperEvaluationInfor) {
        typeListPosition = PaperEvaluationInforIndexHelp.QuesPosition_return_TypeListPosition(paperEvaluationInfor, vpQuesPosition)
        val typeIndex = paperEvaluationInfor.TypeList[typeListPosition].TypeIndex
        val strTypeIndex = LancooMethod.numberToChinese(typeIndex)
        var genreName = paperEvaluationInfor.TypeList[typeListPosition].GenreName
        val typeName = paperEvaluationInfor.TypeList[typeListPosition].TypeName
        if (genreName.isEmpty()) {
            genreName = typeName
        }
        /******************************************************************************************/
        var totalChildQuesSize = 0
        var totalChildQuesScore = 0.0
        val quesListSize = paperEvaluationInfor.TypeList[typeListPosition].QuesList.size
        for (i in 0 until quesListSize) {
            val sortIndexListSize = paperEvaluationInfor.TypeList[typeListPosition].QuesList[i].SortScoreRateList.size
            totalChildQuesSize += sortIndexListSize

            for (j in 0 until sortIndexListSize) {
                val score = paperEvaluationInfor.TypeList[typeListPosition].QuesList[i].SortScoreRateList[j].Score
                totalChildQuesScore += score
            }
        }

        var isAverage = true
        val averageScore = LancooMethod.getCorrectRateInt(totalChildQuesScore, totalChildQuesSize)
        outLoop@ for (i in 0 until quesListSize) {
            val sortIndexListSize = paperEvaluationInfor.TypeList[typeListPosition].QuesList[i].SortScoreRateList.size
            for (j in 0 until sortIndexListSize) {
                val score = paperEvaluationInfor.TypeList[typeListPosition].QuesList[i].SortScoreRateList[j].Score
                if (score != averageScore) {
                    isAverage = false
                    break@outLoop
                }
            }
        }

            if (isAverage) {
                val strAverageScore = LancooMethod.killlingDouble(averageScore)
                TvQuesInfor.text = Html.fromHtml(resources.getString(R.string.aibk_practice_ques_infor_short,
                        strTypeIndex, genreName, quesListSize,totalChildQuesSize, strAverageScore))
            } else {
                var childScoreAll = ""
                val quesListSize = paperEvaluationInfor.TypeList[typeListPosition].QuesList.size
                for (x in 0 until quesListSize) {
                    val sortIndexListSize = paperEvaluationInfor.TypeList[typeListPosition].QuesList[x].SortScoreRateList.size
                    for (y in 0 until sortIndexListSize) {
                        val sortIndex = paperEvaluationInfor.TypeList[typeListPosition].QuesList[x].SortScoreRateList[y].SortIndex
                        val score = paperEvaluationInfor.TypeList[typeListPosition].QuesList[x].SortScoreRateList[y].Score
                        val strScore = LancooMethod.killlingDouble(score)
                        val childScore = resources.getString(R.string.aibk_single_ques_infor, sortIndex, strScore)
                        if (childScoreAll.isEmpty()) {
                            childScoreAll = childScore
                        } else {
                            childScoreAll = "$childScoreAll,$childScore"
                        }
                    }
                }
                TvQuesInfor.text = Html.fromHtml(resources.getString(R.string.aibk_practice_ques_infor_long,
                        strTypeIndex, genreName,quesListSize, totalChildQuesSize, childScoreAll))
            }
        /******************************************************************************************/

    }


    //实现答题卡弹窗
    private fun showAnswerSheetDialog() {
        val dialog = AibkEvaluationAnswerSheetDialog(this, paperEvaluationInfor!!, typeListPosition)
        dialog.show()
        dialog.setClickDialogListener(object : AibkEvaluationAnswerSheetDialog.OnClickDialogListener {

            override fun onSortIndexClick(sortIndex: String) {
                dialog.dismiss()
                var parentVpPosition = PaperEvaluationInforIndexHelp.SortIndex_return_ParentVpPosition(paperEvaluationInfor!!, sortIndex)
                VpEvaluation.setCurrentItem(parentVpPosition, true)

                var indexA = PaperEvaluationInforIndexHelp.SortIndex_return_IndexA(paperEvaluationInfor!!, sortIndex)
                var typeNo = paperEvaluationInfor!!.TypeList[indexA].TypeNo
                if (typeNo == "k") {//k：阅读综合
                    val fragment = fragmentList!![parentVpPosition] as EvaluationFragmentZht
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "x" || typeNo == "m") {//x:听力匹配,m:普通匹配题
                    val fragment = fragmentList!![parentVpPosition] as EvaluationFragmentPpt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "L" ||typeNo == "E" || typeNo == "F" || typeNo == "D") {//听力选择，听力判断，阅读选择
                    val fragment = fragmentList!![parentVpPosition] as EvaluationFragmentXzt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "Q" || typeNo == "b" || typeNo == "d"
                        || typeNo == "X" || typeNo == "g" || typeNo == "V"
                        || typeNo == "G") {//Q:阅读填空,X:阅读简答,G:听力简答,
                    val fragment = fragmentList!![parentVpPosition] as EvaluationFragmentJdt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                }else if (typeNo == "H") {//H:复合写作
                    val fragment = fragmentList!![parentVpPosition] as EvaluationFragmentFhxz
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "C") {//C:完形选择
                    val fragment = fragmentList!![parentVpPosition] as EvaluationFragmentWxxz
                    fragment.fragmentJumpToSortIndex(sortIndex)
                } else if (typeNo == "N" || typeNo == "S") { //N:普通填空题  S：听力填空题
                    val fragment = fragmentList!![parentVpPosition] as EvaluationFragmentTkt
                    fragment.fragmentJumpToSortIndex(sortIndex)
                }

            }

            override fun onDismissClick() {
                dialog.dismiss()
            }

        })
    }

    override fun onPause() {
        super.onPause()
        toolPauseAudio()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.BtnBack -> {
                toolPauseAudio()
                finish()
            }
            R.id.BtnErrorRecovery -> {
                toolPauseAudio()
                quesListPosition = PaperEvaluationInforIndexHelp.QuesPosition_return_QuesListPosition(paperEvaluationInfor!!, vpQuesPosition)
                val quesId = paperEvaluationInfor!!.TypeList[typeListPosition].QuesList[quesListPosition].QuesID
                ActivityErrorRecovery.launch(this,quesId)
            }
            R.id.BtnExport -> {
                toolPauseAudio()
                val dialog = DialogEvaluationExport(this, this, paperEvaluationInfor!!)
                dialog.setClickExportListener(object : DialogEvaluationExport.OnClickExportListener {
                    override fun onExportEva() {
                        exportEvaPaper(dialog)
                    }
                })
                dialog.show()
            }
            R.id.BtnKnowledge -> {
                toolPauseAudio()
                quesListPosition = PaperEvaluationInforIndexHelp.QuesPosition_return_QuesListPosition(paperEvaluationInfor!!, vpQuesPosition)
                JumpActivityHelp.paperEvaluationJumpToKnowledgePointActivity(this, paperEvaluationInfor!!, typeListPosition, quesListPosition)
            }
            R.id.BtnAnswerSheet -> {
                toolPauseAudio()
                showAnswerSheetDialog()
            }
            R.id.TvQuesInfor ->{
                quesScoreInfor =  TvQuesInfor.text.toString()
                val mTextPaint: TextPaint = TvQuesInfor.paint
                mTextPaint.textSize = TvQuesInfor.textSize
                val mTextViewWidth = mTextPaint.measureText(quesScoreInfor).toInt()
                val mTvQuesInforWidth = TvQuesInfor.width - DensityUtils.dp2px(this,20f)
                if (mTextViewWidth > mTvQuesInforWidth) { //超出一行
                    ShowDialogTools.showQuesScoreInofrDialog(this, quesScoreInfor)
                }
            }
        }
    }

    //导出讲评试卷
    private fun exportEvaPaper(dialog: DialogEvaluationExport) {
        val paperId = intent.getStringExtra("PaperID")
        val setId = intent.getStringExtra("SetID")
        val isAnswerAnalysis = paperEvaluationInfor!!.CommentExportTags.AnswerAnalysis
        val isZsd = paperEvaluationInfor!!.CommentExportTags.Zsd
        val isListenVideo = paperEvaluationInfor!!.CommentExportTags.ListenVideo
        val isListenText = paperEvaluationInfor!!.CommentExportTags.ListenText
        mViewModel.exportComment(paperId,setId, isAnswerAnalysis, isZsd, isListenVideo, isListenText)
                .doOnSubscribe {
                    dialog.LlExportLoadingView.visibility = View.VISIBLE
                    dialog.LlExportContentView.visibility = View.GONE
                }.bindLifeCycle(this)
                .subscribe({
                    if (it.code == 1) {
                        Thread(Runnable {
                            val localEvaPath = getExternalFilesDir("")?.getAbsolutePath() + "/aibk/EvaZip/"
                            DownLoadEvaZip(dialog, it.data, localEvaPath)
                        }).start()
                    } else {
                        dialog.dismiss()
                        MyToast.showToast("导出异常")
                    }

                }, {
                    MyToast.showToast(it.message)
                    dialog.dismiss()
                })

    }

    /*
    * 获取纠错类型
    * */
    fun getErrorConfig() {
        mViewModel.getErrorConfig()
                .doOnSubscribe {
                }.bindLifeCycle(this)
                .subscribe({
                    configListener!!.getErrorRecoverySuccess(it.data)
                }, {
                    configListener!!.getErrorRecoveryFaile(it.message.toString())
                })
    }

    interface OnConfigListener {
        fun getErrorRecoverySuccess(errorRecovery: List<ErrorRecovery>)
        fun getErrorRecoveryFaile(errorMessage: String)
    }

    private var configListener: OnConfigListener? = null

    fun setConfigListener(configListener: OnConfigListener?) {
        this.configListener = configListener
    }

    //暂停一切音频播放
    private fun toolPauseAudio() {
        var typeNo = paperEvaluationInfor!!.TypeList[typeListPosition].TypeNo
        if (typeNo == "x") {//x:听力匹配
            val fragment = fragmentList!![vpQuesPosition] as EvaluationFragmentPpt
            fragment.pauseAudio()
            fragment.pauseChildAudio()
            fragment.audioTrackerStop()
        } else if (typeNo == "L" ||typeNo == "E" || typeNo == "F") {//听力选择，听力判断
            val fragment = fragmentList!![vpQuesPosition] as EvaluationFragmentXzt
            fragment.pauseAudio()
            fragment.pauseChildAudio()
            fragment.audioTrackerStop()
        } else if (typeNo == "G") {//G:听力简答,
            val fragment = fragmentList!![vpQuesPosition] as EvaluationFragmentJdt
            fragment.pauseAudio()
            fragment.pauseChildAudio()
            fragment.audioTrackerStop()
        } else if (typeNo == "S") { // S：听力填空题
            val fragment = fragmentList!![vpQuesPosition] as EvaluationFragmentTkt
            fragment.pauseAudio()
            fragment.pauseChildAudio()
            fragment.audioTrackerStop()
        } else if (typeNo == "f") {
            val fragment = fragmentList!![vpQuesPosition] as EvaluationFragmentZw
            fragment.pauseAudio()
            fragment.pauseChildAudio()
            fragment.audioTrackerStop()
        } else if (typeNo == "H") {
            val fragment = fragmentList!![vpQuesPosition] as EvaluationFragmentFhxz
            fragment.pauseAudio()
            fragment.pauseChildAudio()
            fragment.audioTrackerStop()
        }
    }

    //下载试卷讲评内容到本地
    fun DownLoadEvaZip(dialog: DialogEvaluationExport, downLoadPath: String, localEvaPath: String): Boolean {
        var inputStream: InputStream? = null
        var fileOutputStream: FileOutputStream? = null
        var httpURLConnection: HttpURLConnection? = null
        if (Environment.MEDIA_MOUNTED != Environment.getExternalStorageState()) {
            Log.i("下载Audio压缩文件", " 未安装 SD 卡")
            return false
        }
        val pathSd = Environment.getExternalStorageDirectory() //获取 SD 卡路径
        val fileRealDirectoty = File(pathSd.absolutePath + File.separator + localEvaPath)
        Log.i("下载Audio压缩文件", "文件保存的真正目录： $fileRealDirectoty")
        val shareFileLocalPath = fileRealDirectoty.toString() + "/" + downLoadPath.substring(downLoadPath.lastIndexOf("/"))
        if (!fileRealDirectoty.exists()) {  //如果目录 不存在 ，就创建
            Log.i("下载Audio压缩文件", "创建 存储文件夹")
            fileRealDirectoty.mkdirs()
        }
        //获取要下载的文件名称，在这里可以更改下载的文件名
        val fileName = downLoadPath.substring(downLoadPath.lastIndexOf("/") + 1)
        //String fileName = "test.zip";
        //创建 这个文件名 命名的 file 对象
        val file = File(fileRealDirectoty, fileName)
        Log.i("下载Audio压缩文件", "file: $file")
        if (!file.exists()) {  //倘若没有这个文件
            try {
                Log.i("下载Audio压缩文件", "创建文件")
                file.createNewFile() //创建这个文件
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        try {
            val url = URL(downLoadPath)
            httpURLConnection = url.openConnection() as HttpURLConnection
            httpURLConnection.connect()
            val code = httpURLConnection!!.responseCode
            if (code == 200) {
                //网络连接成功
            } else {
                //网络返回异常
                MyToast.showToast("网络异常，导出试卷失败")
                dialog.dismiss()
            }
            //根据响应获取文件大小
            val fileSize = httpURLConnection.contentLength
            Log.i("下载Audio压缩文件", "文件大小： $fileSize")
            inputStream = httpURLConnection.inputStream
            fileOutputStream = FileOutputStream(file)
            val b = ByteArray(1024)
            var tem = 0
            while (inputStream.read(b).also { tem = it } != -1) {
                fileOutputStream.write(b, 0, tem)
                Log.i("下载Audio压缩文件", "文件下载进度=======" + b.size)
            }
            Log.i("下载Audio压缩文件", "文件下载 成功")
            if (dialog.isShowing) {
                dialog.dismiss()
                ExportUtil.shareFile(this, shareFileLocalPath, getHolderActivity())
            }
        } catch (e: MalformedURLException) {
            e.printStackTrace()
            return false
        } catch (e: IOException) {
            e.printStackTrace()
            return false
        } finally {
            try {
                inputStream?.close()
                fileOutputStream?.close()
                httpURLConnection?.disconnect()
            } catch (e: IOException) {
                e.printStackTrace()
            }
        }
        return true
    }
}